package com.tencent.albert.weixin.driver.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.tencent.albert.weixin.driver.bean.BaiduShortURL;

import java.io.IOException;

/**
 * Created by Jason on 2015/10/16.
 */
public class URLUtil {

    /**
     * 获取短连接
     * @param longUrl
     * @return
     * @throws UnirestException
     * @throws IOException
     */
    public static String getShortUrl(String longUrl) throws UnirestException, IOException {
        String url = "http://dwz.cn/create.php";
        HttpResponse<JsonNode> response = null;
        response = Unirest.post(url).field("url", longUrl).asJson();
        String result = response.getBody().toString();

        ObjectMapper objectMapper = new ObjectMapper();
        BaiduShortURL baiduShortURL = objectMapper.readValue(result, BaiduShortURL.class);

        String shortUrl = baiduShortURL.getTinyurl();
        return shortUrl;
    }

    public static void main(String[] args) throws Exception {
        String url = getShortUrl("http://www.52xiaoluo.com/");
        System.out.println(url);

//        byte[] sDefaultKey = "nealkeys&&&&keys".getBytes();
//        byte[] sDefaultIv={0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0x30,0x31,0x32,0x33,0x34,0x35};
//
//        String value="livingtestKey";
//        byte[] retByte= EcryptionUtil.AES.encrypt(value.getBytes(),sDefaultKey,sDefaultIv);
//        String ret= Base64Util.encodeToString(retByte, Base64Util.NO_PADDING | Base64Util.NO_WRAP | Base64Util.URL_SAFE);
//
//        System.out.println(ret);
    }
}
